<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

    <img id="tu" src="img/1.jpg" width="20%">
    <br>
    <button onclick="begin()">开始</button>
    <button onclick="stop()">停止</button>

    <script>
        var arr=["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"];
        function begin(){
            timer=setInterval(bian,100); //没有使用var 所以timer是全局变量
        }

        function stop(){
            clearInterval(timer); //停止定时器
        }

        function bian(){
            var i=Math.floor(Math.random()*arr.length); //0-4 
            document.getElementById("tu").src="img/"+arr[i];
        }
      
    </script>
</body>
</html>